import app from '@system.app';
const BUTTON_STATE_IMAGE = ['/common/images/checkbox-blank.png', '/common/images/checkbox-circle.png'];
export default {
    data: {
        "busList": [
            {"id": 1, "busNum": "987路", "waitingStation": "海珠客运站总站", "endStation": "天安科技园总站", "timeRemaining": 5, "stationsRemaining": 1, "kmRemaining": 1.5, "checkBtn": BUTTON_STATE_IMAGE[0]},
            {"id": 2, "busNum": "303路", "waitingStation": "太古仓路总站", "endStation": "海珠客运站总站", "timeRemaining": 20, "stationsRemaining": 3, "kmRemaining": 3.5, "checkBtn": BUTTON_STATE_IMAGE[0]},
            {"id": 3, "busNum": "288路", "waitingStation": "大夫山公园总站", "endStation": "洛溪新城总站", "timeRemaining": 20, "stationsRemaining": 3, "kmRemaining": 3.5, "checkBtn": BUTTON_STATE_IMAGE[0]},
            {"id": 4, "busNum": "129路", "waitingStation": "洛溪新城总站", "endStation": "奥林匹克花园总站", "timeRemaining": 20, "stationsRemaining": 3, "kmRemaining": 3.5, "checkBtn": BUTTON_STATE_IMAGE[0]},
            {"id": 5, "busNum": "230路", "waitingStation": "华工大总站", "endStation": "客村立交总站", "timeRemaining": 20, "stationsRemaining": 3, "kmRemaining": 3.5, "checkBtn": BUTTON_STATE_IMAGE[0]},
            {"id": 6, "busNum": "882路", "waitingStation": "客村立交总站", "endStation": "员村总站", "timeRemaining": 20, "stationsRemaining": 3, "kmRemaining": 3.5, "checkBtn": BUTTON_STATE_IMAGE[0]}
        ],
        "keyword": "",
        "busId": -1
    },
    onInit() {
        this.getBusList();
    },
    initAction: function (code, actionData) {
        var action = {};
        action.bundleName = "com.army.study";
        action.abilityName = "com.army.study.service.BusInternalAbility";
        action.messageCode = code;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = 0;
        return action;
    },
    getBusList: async function () {
        var actionData = {"keyword": this.keyword};
        try {
            var action = this.initAction(1001, actionData);
            var result = await FeatureAbility.callAbility(action);
            console.info(" @@result = " + JSON.stringify(result));
            this.busList = JSON.parse(result);
        } catch (pluginError) {
            console.error("getBusList : Plugin Error = " + pluginError);
        }
    },
    change(e) {
        this.keyword = e.value;
        this.getBusList();
    },
    bindingCard: async function () {
        var actionData = {"busId": this.busId};
        try {
            var action = this.initAction(1003, actionData);
            var result = await FeatureAbility.callAbility(action);
            console.info(" @@result = " + result);

            if("OK" == result) {
                // 延时500毫秒关闭当前页面
                setTimeout(function(){app.terminate();},500);
            }
        } catch (pluginError) {
            console.error("bindingCard : Plugin Error = " + pluginError);
        }
    },
    checked(id) {
        let _this = this;
        _this.busList.forEach(element => {
            element.checkBtn =  element.id == id ? BUTTON_STATE_IMAGE[1] : BUTTON_STATE_IMAGE[0]
        });

        this.busId = id;
        this.bindingCard();
    }
}
